package com.service;

import com.dao.BookStockDao;
import com.dto.BookDTO;
import com.dto.BookStockDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.po.BookStockPO;
import com.util.RestException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author linyukun
 * @date 2022/7/14 10:14
 */
@Service
public class BookStockImpl {
    @Autowired
    private BookStockDao bookStockDao;

    public Object saveBookStockByBookId(BookStockDTO dto) {
        if(ObjectUtils.isEmpty(dto.getBookId())){
            throw new RestException("请传递书籍id");
        }
        if(StringUtils.isBlank(dto.getIndexBookNumber())){
            throw new RestException("索书号不能为空");
        }
        if(StringUtils.isBlank(dto.getTicketNumber())){
            throw new RestException("条码不能为空");
        }
        BookStockPO bookStockPO = bookStockDao.getBookStockByticketNumber(dto);

        if(ObjectUtils.isEmpty(dto.getId())){//如果id是空的
            if(!ObjectUtils.isEmpty(bookStockPO)){
                throw new RestException("该条码已经存在了");
            }
            bookStockDao.insertBookStock(dto);
        }else{
            if(!ObjectUtils.isEmpty(bookStockPO) && !bookStockPO.getId().equals(dto.getId())){
                throw new RestException("该条码已经存在了");
            }
            //判断状态
            bookStockDao.updateBookStock(dto);
        }
        return null;
    }

    public PageInfo getBookStockListByBookId(BookStockDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo(bookStockDao.getBookStockListByBookId(dto));
    }

    public Object delBookStockById(BookStockDTO dto) {
        return bookStockDao.delBookStockById(dto);
    }


    public BookStockPO getBookStockByTicketNumber(BookStockDTO dto) {
        return bookStockDao.getBookStockByticketNumber(dto);
    }
}

